// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: SCZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.palRotateTimer
private alias object.value2 : object.palRotateIndex


// Player Aliases
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.controlMode : player.controlMode
private alias object.tileCollisions : player.tileCollisions
private alias object.collisionRight : player.collisionRight
private alias object.jumpPress : player.jumpPress
private alias object.jumpHold : player.jumpHold
private alias object.up	: player.up
private alias object.down : player.down
private alias object.left : player.left
private alias object.right : player.right

private alias object.value0 : player.rings

// Game Modes
private alias 0 : MODE_NOSAVE
private alias 1 : MODE_SAVEGAME
private alias 2 : MODE_TIMEATTACK

// Tracks
private alias 0 : TRACK_STAGE

// Reserved object slots
private alias 10 : SLOT_ZONESETUP

// Music Loops
private alias 198253 : MUSIC_LOOP_SCZ


// ========================
// Tables
// ========================

private table SCZSetup_palRotationTable
	0xE0E040, 0xE0A020, 0xE06000, 0xC02000
	0xE0E0E0, 0xE0E040, 0xE0A020, 0xE06000
	0xE0E040, 0xE0A020, 0xE06000, 0xC02000
	0xE0A020, 0xE06000, 0xC02000, 0x802000
end table


// ========================
// Events
// ========================

event ObjectUpdate
	object.palRotateTimer++
	if object.palRotateTimer == 2
		object.palRotateTimer = 0

		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 167, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15
		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 168, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15
		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 169, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15
		GetTableValue(temp0, object.palRotateIndex, SCZSetup_palRotationTable)
		SetPaletteEntry(0, 170, temp0)

		object.palRotateIndex++
		object.palRotateIndex &= 15

	

#platform: USE_ORIGINS
			stage.timeEnabled = false
			CallNativeFunction2(NotifyCallback, NOTIFY_ACT_FINISH, 0)
			if game.oneStageFlag != false
				game.callbackResult = -1
				CallNativeFunction4(NotifyCallback, NOTIFY_STAGE_RETRY, 1, stage.listPos, 0)
				object.state = SCZSETUP_STATIC
			end if
#endplatform
		end if
		break

	case SCZSETUP_FADEOUT // fade to WFZ
		player[0].yvel = 0
		object.timer += 4
		music.volume -= 2
		SetScreenFade(0, 0, 0, object.timer)
		if object.timer == 320
			fadeColor = 0x000000
			object.timer = 0
			starPostID = 0
			
			// This object's direction is never used, is it?
			// I don't think this does anything...
			object.direction = FACING_RIGHT
			
			stage.listPos++

			if options.gameMode == MODE_SAVEGAME
				arrayPos1 = options.saveSlot
				arrayPos1 <<= 3
				if stage.player2Enabled == true
#platform: USE_STANDALONE
					saveRAM[arrayPos1] = PLAYER_SONIC_TAILS_A
#endplatform
#platform: USE_ORIGINS
					switch stage.playerListPos
					case PLAYER_SONIC_A
						saveRAM[arrayPos1] = PLAYER_SONIC_TAILS
						break
						
					case PLAYER_KNUCKLES_A
						saveRAM[arrayPos1] = PLAYER_KNUCKLES_TAILS
						break
						
					case PLAYER_AMY_A
						saveRAM[arrayPos1] = PLAYER_AMY_TAILS
						break
					end switch
#endplatform
				else
					saveRAM[arrayPos1] = stage.playerListPos
				end if
				arrayPos1++
				saveRAM[arrayPos1] = player.lives
				arrayPos1++
				saveRAM[arrayPos1] = player.score
				arrayPos1++
				saveRAM[arrayPos1] = player.scoreBonus
				arrayPos1++
				
				// Bug Details:
				// This is missing the check for if your save file is already past the next act
				// This means the save file's act will be overwritten to Wing Fortress Zone, even if you're on a Complete save file...
				saveRAM[arrayPos1] = stage.listPos
				saveRAM[arrayPos1]++
				
				arrayPos1++
				saveRAM[arrayPos1] = specialStage.emeralds
				arrayPos1++
				saveRAM[arrayPos1] = specialStage.listPos
				WriteSaveRAM()
			end if

			// ???
			// you cannot speedrun sky chase.
			if options.gameMode == MODE_TIMEATTACK
				timeAttack.result = stage.seconds
				timeAttack.result *= 100
				temp0 = stage.minutes
				temp0 *= 6000
				timeAttack.result += temp0
				timeAttack.result += stage.milliSeconds
				stage.listPos--
				CallNativeFunction2(SetLeaderboard, stage.listPos, timeAttack.result)
				engine.state = 8
			else
				temp0 = engine.trialMode
				if stage.listPos >= stage.listSize
					temp0 = true
				end if

				if temp0 == false
#platform: USE_ORIGINS
					CallNativeFunction4(NotifyCallback, NOTIFY_STATS_ENEMY, StageStatsUsabilityParam1, StageStatsUsabilityParam2, StageStatsUsabilityParam3)
					CallNativeFunction2(NotifyCallback, NOTIFY_STATS_RING, player[0].rings)
					temp0 = StageStatsUsabilityParam5
					temp0 &= 1
					temp1 = StageStatsUsabilityParam5
					temp1 &= 2
					temp2 = StageStatsUsabilityParam5
					temp2 &= 4
					CallNativeFunction4(NotifyCallback, NOTIFY_STATS_CHARA_ACTION, temp0, temp1, temp2)
					CallNativeFunction2(NotifyCallback, NOTIFY_ACT_FINISH, 0)
					StageStatsUsabilityParam1 = 0
					StageStatsUsabilityParam2 = 0
					StageStatsUsabilityParam3 = 0
					StageStatsUsabilityParam4 = 0
					StageStatsUsabilityParam5 = 0
					StatsUsabilityParam1 = 0
					StatsUsabilityParam2 = 0
					StatsUsabilityParam3 = 0
					StatsUsabilityParam4 = 0
					StatsUsabilityParam5 = 0
#endplatform

					LoadStage()
				else
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = 0
					LoadStage()
				end if
			end if
		end if
		break

	end switch

	// R Boundary
	if object.state < SCZSETUP_TOWINGFORTRESS
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += player[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				player[currentPlayer].xvel = 0
				player[currentPlayer].speed = 0
				player[currentPlayer].xpos = temp1
				temp0 = player[currentPlayer].collisionRight
				temp0 <<= 16
				player[currentPlayer].xpos -= temp0
			end if
		next
	end if

	SCZSetup_hPropellerFrame = SCZSetup_hPropellerTimer
	SCZSetup_hPropellerFrame >>= 1
	SCZSetup_hPropellerTimer++
	SCZSetup_hPropellerTimer %= 12

	SCZSetup_vPropellerFrame = SCZSetup_vPropellerTimer
	SCZSetup_vPropellerFrame >>= 1
	SCZSetup_vPropellerTimer++
	SCZSetup_vPropellerTimer %= 6
end event


event ObjectStartup
	SetMusicTrack("SkyChase.ogg", TRACK_STAGE, MUSIC_LOOP_SCZ)

	// Set the "inhabitants" of this zone
	// I sure hope they can fly...
	animalType1 = TypeName[Cucky]
	animalType2 = TypeName[Tocky]

	object[SLOT_ZONESETUP].type = TypeName[SCZ Setup]
	object[SLOT_ZONESETUP].priority = PRIORITY_ACTIVE

	SCZSetup_scrollPos.x = screen.xcenter
	SCZSetup_scrollPos.x *= -0x10000
	SCZSetup_scrollPos.x += player[0].xpos

	SCZSetup_scrollPos.y = 0

	SCZSetup_scrollVelocity.x = 0x10000
	SCZSetup_scrollVelocity.y = 0
	
	SCZSetup_hPropellerFrame = 0
	SCZSetup_hPropellerTimer = 0
	SCZSetup_vPropellerFrame = 0
	SCZSetup_vPropellerTimer = 0

end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
